package com.mobile.collect.present;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.mobile.collect.R;
import com.mobile.collect.activitys.AddUnitPointActivity;
import com.mobile.collect.beans.DevicePointInfo;
import com.mobile.collect.beans.PointGroupViewInfo;
import com.mobile.collect.beans.PointViewInfo;
import com.mobile.collect.db.EntityManager;
import com.mobile.collect.dialogs.v3.TipDialog;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.droidlover.xdroidmvp.kit.NLog;

public class AddUnitPointPresent extends BasePresent<AddUnitPointActivity> implements PointViewInfo.AddPointListener {

    //    String defaultDesc = "";//附加描述
    String defaultHigh = "";//高度
    String defaultKeepWarm = "";//保温
    List<PointGroupViewInfo> groupList = new ArrayList<>();


    boolean isUpdate;
    Long deviceId;
    Long picDbId;
    Long pointDbId;
    float scaleX;
    float scaleY;
    DevicePointInfo targetInfo;
    public void init(boolean isUpdate,Long deviceId, Long picDbId, Long pointDbId) {
        this.isUpdate = isUpdate;
        this.deviceId = deviceId;
        this.picDbId = picDbId;
        this.pointDbId = pointDbId;
        if (null != pointDbId) {
            targetInfo = EntityManager.getInstance().queryDevicePointInfoIById(pointDbId);
            if (null != targetInfo) {
                this.scaleX = targetInfo.getScaleX();
                this.scaleY = targetInfo.getScaleY();
            }
        }
    }


    public View getChildGroupView(boolean isUpdate, DevicePointInfo devicePointInfo) {
        LayoutInflater layoutInflater = LayoutInflater.from(getV());
        View childView = layoutInflater.inflate(R.layout.item_pointgroup, null, false);
        PointGroupViewInfo info;
        info = new PointGroupViewInfo(childView);
        if (devicePointInfo != null) {
            info.setmView(childView, devicePointInfo.getStatus(), devicePointInfo.getHeight(), devicePointInfo.getCheckHardType(), devicePointInfo.getCheckHardReason(), devicePointInfo.getDangerCheck(), devicePointInfo.getHeatPreservation(), devicePointInfo.getAdditionalDes());
        } else {
            int size = groupList.size();
            if (size != 0) {
                PointGroupViewInfo lastViewInfo = groupList.get(size - 1);
                String status = lastViewInfo.getStatusTv().getText().toString();
                String high = lastViewInfo.getHighTv().getText().toString();
                String checkHardType = lastViewInfo.getHardTv().getText().toString();
                String checkHardReason = lastViewInfo.getHardeasonTv().getText().toString();
                String dangerCheck = lastViewInfo.getDangerTv().getText().toString();
                String HeatPreservation = lastViewInfo.getKeepWarmTv().getText().toString();
                String desc = lastViewInfo.getDescTv().getText().toString();
                info.setmView(childView, status, high, checkHardType, checkHardReason, dangerCheck, HeatPreservation, desc);
            } else {
                info.setmView(childView);
            }
        }
        PointViewInfo pointViewInfo;
        if (isUpdate) {
            pointViewInfo = getPointView(devicePointInfo);
        } else if (devicePointInfo != null) {
            pointViewInfo = getPointView("", devicePointInfo.getDiameter(), devicePointInfo.getDeviceCount());
        } else {
            pointViewInfo = getPointView("", "", 1);
        }
        List<PointViewInfo> childList = new ArrayList<>();
        info.setList(childList);
        childList.add(pointViewInfo);
        info.addPointView(pointViewInfo.getmView());
        groupList.add(info);
        return childView;
    }

    private PointViewInfo getPointView(DevicePointInfo devicePointInfo) {
        LayoutInflater layoutInflater = LayoutInflater.from(getV());
        View childView = layoutInflater.inflate(R.layout.item_childpoint, null, false);
        PointViewInfo info;
        info = new PointViewInfo(devicePointInfo.get_ID(), this);
        info.setmView(childView, devicePointInfo.getChildTypeEn(), devicePointInfo.getDiameter(), devicePointInfo.getDeviceCount());
        info.setChildType(devicePointInfo.getChildType());
        info.setChildTypeEn(devicePointInfo.getChildTypeEn());
        info.setCount(devicePointInfo.getDeviceCount());
        info.setTypeEn(devicePointInfo.getTypeEn());
        info.setType(devicePointInfo.getType());
        return info;
    }

    private PointViewInfo getPointView(Long updateId, String childTypeEn, String dimen, int count) {
        LayoutInflater layoutInflater = LayoutInflater.from(getV());
        View childView = layoutInflater.inflate(R.layout.item_childpoint, null, false);
        PointViewInfo info;
        if (updateId != null) {
            info = new PointViewInfo(updateId, this);
        } else {
            info = new PointViewInfo(this);
        }
        info.setmView(childView, childTypeEn, dimen, count);
        return info;
    }

    private PointViewInfo getPointView(String childTypeEn, String dimen, int count) {
        return getPointView(null, childTypeEn, dimen, count);
    }

    public void setDefaultHigh(String defaultHigh) {
        this.defaultHigh = defaultHigh;
    }

    public void setDefaultKeepWarm(String defaultKeepWarm) {
        this.defaultKeepWarm = defaultKeepWarm;
    }

    public void startSave() {
        List<DevicePointInfo> pointList = checkInput();
        if (pointList.size() != 0) {
            saveAllPoint(pointList);
        }
    }


    private List<DevicePointInfo> checkInput() {
        List<DevicePointInfo> pointList = new ArrayList<>();
        for (PointGroupViewInfo info : groupList) {
            String status = info.getStatusTv().getText().toString();
            String high = info.getHighTv().getText().toString();
            String keepwarm = info.getKeepWarmTv().getText().toString();
            String desc = info.getDescTv().getText().toString();
            String hard = info.getHardTv().getText().toString();
            String danger = info.getDangerTv().getText().toString();
            String hardreason = info.getHardeasonTv().getText().toString();

            if (TextUtils.isEmpty(status)) {
                TipDialog.show(getV(), "请选择物料状态", TipDialog.TYPE.WARNING);
                break;
            }
            if (TextUtils.isEmpty(hard)) {
                TipDialog.show(getV(), "难于检测不能为空", TipDialog.TYPE.WARNING);
                break;
            }
            if (TextUtils.isEmpty(danger)) {
                TipDialog.show(getV(), "险于检测不能为空", TipDialog.TYPE.WARNING);
                break;
            }

            if (hard.equals("是") && TextUtils.isEmpty(hardreason)) {
                TipDialog.show(getV(), "请输入难于检测原因", TipDialog.TYPE.WARNING);
                break;
            }

            if (TextUtils.isEmpty(high)) {
                TipDialog.show(getV(), "密封点高度不能为空", TipDialog.TYPE.WARNING);
                break;
            }
            if (TextUtils.isEmpty(keepwarm)) {
                TipDialog.show(getV(), "密封点保温不能为空", TipDialog.TYPE.WARNING);
                break;
            }
            if (TextUtils.isEmpty(desc)) {
                TipDialog.show(getV(), "密封点附加描述不能为空", TipDialog.TYPE.WARNING);
                break;
            }
            List<DevicePointInfo> childList = checkChildInput(info);
            if (null == childList) {
                pointList.clear();
                break;
            } else {
                pointList.addAll(childList);
            }
        }
        return pointList;
    }

    private List<DevicePointInfo> checkChildInput(PointGroupViewInfo pointGroupViewInfo) {
        List<PointViewInfo> childList = pointGroupViewInfo.getList();
        List<DevicePointInfo> list = new ArrayList<>();

        for (PointViewInfo pointViewInfo : childList) {
            String childType = pointViewInfo.getChildTypeTv().getText().toString();
            String dimen = pointViewInfo.getDimenTv().getText().toString();
            String count = pointViewInfo.getNumberTv().getText().toString();
            if (TextUtils.isEmpty(childType)) {
                TipDialog.show(getV(), "密封点类型不能为空", TipDialog.TYPE.WARNING);
                return null;
            }
            if (TextUtils.isEmpty(dimen)) {
                TipDialog.show(getV(), "密封点尺寸不能为空", TipDialog.TYPE.WARNING);
                return null;
            }
            if (TextUtils.isEmpty(count) || TextUtils.equals(count, "0")) {
                TipDialog.show(getV(), "密封点尺寸不能为空", TipDialog.TYPE.WARNING);
                return null;
            }

            if (childType.equals("KP") || childType.equals("空冷丝堵")) {
                DevicePointInfo devicePointInfo = getNewDevicePointInfo(pointGroupViewInfo, pointViewInfo, pointViewInfo.getUpdateId());
                devicePointInfo.setDeviceCount(Integer.parseInt(count));
                list.add(devicePointInfo);
            } else {
                Long updateId = pointViewInfo.getUpdateId();
                boolean hadAdd = false;
                for (int r = 0; r < Integer.parseInt(count); r++) {
                    DevicePointInfo pointInfo;
                    if (hadAdd) {
                        pointInfo = getNewDevicePointInfo(pointGroupViewInfo, pointViewInfo, null);
                    } else {
                        hadAdd = true;
                        pointInfo = getNewDevicePointInfo(pointGroupViewInfo, pointViewInfo, updateId);
                    }
                    pointInfo.setDeviceCount(1);
                    list.add(pointInfo);
                }
            }

        }
        return list;
    }

    private DevicePointInfo getNewDevicePointInfo(PointGroupViewInfo pointGroupViewInfo, PointViewInfo pointViewInfo, Long updateId) {
        DevicePointInfo pointInfo = new DevicePointInfo();

        String status = pointGroupViewInfo.getStatusTv().getText().toString();
        String high = pointGroupViewInfo.getHighTv().getText().toString();
        String keepwarm = pointGroupViewInfo.getKeepWarmTv().getText().toString();
        String desc = pointGroupViewInfo.getDescTv().getText().toString();
        String hard = pointGroupViewInfo.getHardTv().getText().toString();
        String danger = pointGroupViewInfo.getDangerTv().getText().toString();
        String hardreason = pointGroupViewInfo.getHardeasonTv().getText().toString();
        pointInfo.setStatus(status);
        pointInfo.setHeight(high);
        pointInfo.setHeatPreservation(keepwarm);
        pointInfo.setAdditionalDes(desc);
        pointInfo.setCheckHardType(hard);
        pointInfo.setDangerCheck(danger);
        pointInfo.setCheckHardReason(hardreason);

        pointInfo.setType(pointViewInfo.getType());
        pointInfo.setTypeEn(pointViewInfo.getTypeEn());
        pointInfo.setChildType(pointViewInfo.getChildType());
        pointInfo.setChildTypeEn(pointViewInfo.getChildTypeEn());
        pointInfo.setDiameter(pointViewInfo.getDimenTv().getText().toString());
        pointInfo.setAdditionalDes(pointGroupViewInfo.getDescTv().getText().toString());
        pointInfo.setHeight(pointGroupViewInfo.getHighTv().getText().toString());
        pointInfo.setHeatPreservation(pointGroupViewInfo.getKeepWarmTv().getText().toString());

        pointInfo.setParentId(picDbId);
        if (null != updateId) {
            pointInfo.set_ID(updateId);
            pointInfo.setLineType2(targetInfo.getLineType2());
            pointInfo.setBgType(targetInfo.getBgType());
            pointInfo.setDirection(targetInfo.getDirection());
        }else{
            pointInfo.setLineType2("1x");
            pointInfo.setBgType(0);
            pointInfo.setDirection(90);
        }
        return pointInfo;
    }

    private void saveAllPoint(List<DevicePointInfo> dataList) {
        int expend = 0;
        for (int i = 0; i < dataList.size(); i++) {
            DevicePointInfo sealPointInfo = dataList.get(i);
            NLog.d("saveAllPoint", sealPointInfo.toString());
            String childTypeEn = sealPointInfo.getChildTypeEn();
            if (sealPointInfo.get_ID() == null) {
                int indexX = i % 10;
                int indexY = i / 10;
                sealPointInfo.setScaleY(0.1f + 0.1f * indexX);
                sealPointInfo.setScaleX(0.1f + 0.1f * indexY);
            }else{
                sealPointInfo.setScaleX(scaleX);
                sealPointInfo.setScaleY(scaleY);
            }
        }
        EntityManager.getInstance().savePointInfo(dataList);
        sendPointChagneEvent(deviceId,picDbId);
        getV().saveSuccess();
//        startActivity(new Intent(this, DevicePointsActivity.class));
    }

    @Override
    public PointViewInfo toAddFanLan(PointViewInfo pointViewInfo) {
        PointViewInfo faLan = getPointView("F", "", 2);
        faLan.setCount(2);
        faLan.setType("法兰");
        faLan.setTypeEn("F");
        faLan.setChildTypeEn("F");
        faLan.setChildType("法兰");
        for (PointGroupViewInfo info : groupList) {
            int index = info.getList().indexOf(pointViewInfo);
            if (index != -1) {
                info.addPointView(faLan, index + 1);
                break;
            }
        }

        return faLan;
    }

    @Override
    public PointViewInfo toAddNewPoint(PointViewInfo pointViewInfo) {
        PointViewInfo faLan = getPointView("", "", 1);
        for (PointGroupViewInfo info : groupList) {
            int index = info.getList().indexOf(pointViewInfo);
            if (index != -1) {
                info.addPointView(faLan, index + 1);
//                info.getList().add(faLan);
                break;
            }
        }

        return faLan;
    }

    @Override
    public PointViewInfo toDelPoint(PointViewInfo pointViewInfo) {
        Iterator<PointGroupViewInfo> iterator = groupList.listIterator();
        while (iterator.hasNext()) {
            PointGroupViewInfo info = iterator.next();
            int index = info.getList().indexOf(pointViewInfo);
            if (index != -1) {
                info.remove(index);
                if (info.getList().size() == 0) {
                    iterator.remove();
                    getV().remove(info.getmView());
                }
                break;
            }
        }
        return null;
    }
}
